package com.javakc.copyrightin.batch.controller;


import com.javakc.commonutils.api.APICODE;
import com.javakc.copyrightin.batch.entity.Batch;
import com.javakc.copyrightin.batch.service.BatchService;
import com.javakc.copyrightin.batch.vo.BatchQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "批次管理")
@RestController
@RequestMapping("/copyrightin/batch")
@CrossOrigin
public class BatchController {
    @Autowired
    private BatchService batchService;

    @ApiOperation(value = "查询批次")
    @GetMapping
    public APICODE findBook() {
        List<Batch> list = batchService.findBatch();
        return APICODE.OK().data("items",list);
    }

    @ApiOperation(value = "根据条件进行分页查询")
    @PostMapping("{pageNo}/{pageSize}")
    public APICODE findPage(
            @ApiParam(name="pageNo",value = "当前页码",required = true) @PathVariable("pageNo") int pageNo,
            @ApiParam(name="pageSize",value = "每页显示的最大条数",required = true) @PathVariable("pageSize") int pageSize,
            @ApiParam(name="batchQuery",value = "查询条件封装对象",required = false) @RequestBody(required = false) BatchQuery batchQuery){
        Page<Batch> page = batchService.findPageBatch(batchQuery, pageNo, pageSize);
        List<Batch> list = page.getContent();
        long totalElements = page.getTotalElements();
        return APICODE.OK().data("items", list).data("total", totalElements);
    }

    @ApiOperation(value = "添加批次")
    @PostMapping("saveBatch")
    public APICODE saveBatch(@RequestBody Batch batch) {
        batchService.saveOrUpdate(batch);
        return APICODE.OK();
    }


    @ApiOperation(value = "修改批次")
    @PutMapping("updateBatch")
    public APICODE updateBatch(@RequestBody Batch batch) {
        batchService.saveOrUpdate(batch);
        return APICODE.OK().data("batch",batch);
    }

    @ApiOperation(value = "根据ID获取书籍")
    @GetMapping("{id}")
    public APICODE view(@PathVariable("id") int id){
        Batch batch = batchService.getById(id);
        return APICODE.OK().data("batch",batch);
    }

    @ApiOperation(value = "根据ID删除批次")
    @DeleteMapping("{id}")
    public APICODE deleteBatch(@PathVariable Integer id) {
        batchService.removeById(id);
        return APICODE.OK();
    }

}
